import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_base/src/ui/widget/dk_bottom_sheet.dart';

class DkBottomSheetDialog<T> {
  BottomSheetOption<T> bottomSheetOption;

  DkBottomSheetDialog.create(this.bottomSheetOption);

  PersistentBottomSheetController<T> controller;

  void show(BuildContext context) {
    switch (bottomSheetOption.bottomSheetType) {
      case BottomSheetType.sheet:
        controller = dkShowBottomSheet<T>(
          context: context,
          builder: bottomSheetOption.isTopCorner
              ? (BuildContext context) {
                  return Stack(
                    children: <Widget>[
                      DkContainer(ContainerParam.withDefault(
                          color: Colors.black54, width: double.infinity)),
                      bottomSheetOption.contentBuilder(context),
                    ],
                  );
                }
              : bottomSheetOption.contentBuilder,
        );
        break;
      case BottomSheetType.sheetPage:
        var future = dkShowModalBottomSheet<T>(
          context: context,
          contentRatio: bottomSheetOption.contentRatio,
          builder: bottomSheetOption.isTopCorner
              ? (BuildContext context) {
                  return Stack(
                    children: <Widget>[
                      DkContainer(ContainerParam.withDefault(
                          color: Colors.black54, width: double.infinity)),
                      bottomSheetOption.contentBuilder(context),
                    ],
                  );
                }
              : bottomSheetOption.contentBuilder,
        );
        if (null != bottomSheetOption.handleResult) {
          future.then((T result) {
            bottomSheetOption.handleResult(result);
          });
        }

        break;
    }
  }

  PersistentBottomSheetController<T> getController() {
    return controller;
  }
}

enum BottomSheetType { sheet, sheetPage }

class BottomSheetOption<T> {
  BottomSheetType bottomSheetType;
  WidgetBuilder contentBuilder;
  HandleResult<T> handleResult;
  bool isTopCorner;
  double contentRatio;

  BottomSheetOption.withSheetBuilder(this.contentBuilder,
      {this.handleResult, this.isTopCorner}) {
    this.bottomSheetType = BottomSheetType.sheet;
    this.isTopCorner ??= false;
  }

  BottomSheetOption.withSheetPageBuilder(this.contentBuilder,
      {this.handleResult, this.isTopCorner, this.contentRatio}) {
    this.bottomSheetType = BottomSheetType.sheetPage;
    this.isTopCorner ??= false;
  }
}
